package com.gscitysfy.cus.drain.modules.duty.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cloudfrmwk.security.utils.SecurityUtils;

import com.gscitysfy.cus.drain.annotations.DictTranslation;
import com.gscitysfy.cus.drain.annotations.FieldParam;
import com.gscitysfy.cus.drain.constants.DicConstant;
import com.gscitysfy.cus.drain.modules.duty.entity.MoperDutyMnExchange;
import com.gscitysfy.cus.drain.modules.duty.entity.MoperDutyMnPeople;
import com.gscitysfy.cus.drain.modules.duty.entity.MoperDutyMnRota;
import com.gscitysfy.cus.drain.modules.duty.mapper.MoperDutyMnRotaMapper;
import com.gscitysfy.cus.drain.modules.duty.service.IMoperDutyMnExchangeService;
import com.gscitysfy.cus.drain.modules.duty.service.IMoperDutyMnPeopleService;
import com.gscitysfy.cus.drain.modules.duty.vo.MoperDutyMnExchangeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 葛守跃
 * @version 1.0.0
 * @ClassName MoperDutyMnExchangeController
 * @Description 值班人员调换班、交接班记录 前端控制器
 * @creatTime 2022-03-29 15:06:16
 */
@RestController
@RequestMapping("/duty/exchange")
@Tag(name = "值班管理->替班、换班管理相关接口")
@Slf4j
public class MoperDutyMnExchangeController {

    @Resource
    private IMoperDutyMnExchangeService exchangeService;

    @Resource
    private MoperDutyMnRotaMapper rotaMapper;

    @Resource
    private IMoperDutyMnPeopleService peopleService;

    /**
     * 保存或者修改调班或者换班记录
     * @param exchange
     * @return
     */
    @PostMapping("/saveOrUpdateExchange")
    @Operation(summary = "保存或者修改调班记录")
    public Result<Boolean> saveOrUpdateExchange(@RequestBody MoperDutyMnExchange exchange){
        //TODO 应该还有一些其他操作
        exchange.setCoamOperateDate(new Date());
        //操作员保存中文姓名
        exchange.setCoamOperatePerson(SecurityUtils.getPrincipals().getName());
        return Result.content(this.exchangeService.saveOrUpdate(exchange));
    }

    /**
     * 删除交换班记录，逻辑删除
     * @param exchange
     * @return
     */
    @PostMapping("/delExchange")
    @Operation(summary = "删除交换班记录")
    public Result<Boolean> delExchange(MoperDutyMnExchange exchange){
        exchange.setCoamUsable(1);
        return Result.content(this.exchangeService.updateById(exchange));
    }

    /**
     * 审核交换班记录
     * @param exchange
     * @return
     */
    @PostMapping("/examineRecord")
    @Operation(summary = "审核交换班记录")
    public Result<Boolean> examineRecord(MoperDutyMnExchange exchange){
        return this.exchangeService.examineRecord(exchange);
    }

    /**
     * 条件查询换班记录
     * @param page
     * @param exchange
     * @return
     */
    @GetMapping("/selectExchangeRecordsByCondition")
    @Operation(summary = "条件查询换班记录")
    public Result<IPage<MoperDutyMnExchange>> selectExchangeRecordsByCondition(Page<MoperDutyMnExchange> page, MoperDutyMnExchangeVo exchange){
        IPage<MoperDutyMnExchange> exchanges =  this.exchangeService.selectExchangeRecordsByCondition(page, exchange);
        return Result.content(exchanges);
    }

    /**
     *  导出替班、换班记录
     * @param response
     * @param exchange
     * @throws IOException
     */
    @GetMapping("/exportExchangeRecords")
    @Operation(summary = "导出值班人员信息")
    public void exportExchangeRecords(HttpServletResponse response, MoperDutyMnExchangeVo exchange) throws IOException {
        this.exchangeService.exportExchangeRecords(response, exchange);
    }

    /**
     * 根据日期和班次，获取此班次所有值班人员
     * @param rota
     * @return
     */
    @GetMapping("/getDutyPersonByDateAndClasses")
    @Operation(summary = "根据日期和班次，获取此班次所有值班人员")
    public Result<List<MoperDutyMnPeople>> getDutyPersonByDateAndClasses(MoperDutyMnRota rota){
        return Result.content(this.rotaMapper.getDutyPersonByDateAndClasses(rota));
    }

    /**
     * 根据日期，获取所有值班人员
     * @param rota
     * @return
     */
    @GetMapping("/getDutyPersonByDate")
    @Operation(summary = "根据日期，获取此班次所有值班人员")
    public Result<IPage<MoperDutyMnPeople>> getDutyPersonByDate(Page<MoperDutyMnPeople> page, MoperDutyMnRota rota){
        return Result.content(this.rotaMapper.getDutyPersonByDate(page, rota));
    }

    /**
     * 查询所有值班人员
     * @param page 分页参数
     * @param rota 值班人员实体
     * @return 分页列表
     */
    @PostMapping("/findDutyPersonByDate")
    @Operation(summary = "查询所有值班人员")
    @DictTranslation({@FieldParam(dictType = DicConstant.DUTY_TYPE, targetField = "coamPersonType")})
    public Result<IPage<MoperDutyMnPeople>> findDutyPersonByDate(Page<MoperDutyMnPeople> page, MoperDutyMnRota rota){
        return Result.content(this.rotaMapper.getDutyPersonByDate(page, rota));
    }


    /**
     * 获取替班人员列表，排除本班次其他值班人员
     * @param page
     * @param people
     * @return
     */
    @GetMapping("/getReplacePeopleListByCondition")
    @Operation(summary = "获取替班人员列表，排除本班次其他值班人员")
    @DictTranslation({@FieldParam(dictType = DicConstant.DUTY_TYPE, targetField = "coamPersonType"),
            @FieldParam(dictType = DicConstant.SYSTEM_FLAG, targetField = "coamSysFlag"),
            @FieldParam(dictType = DicConstant.GENDER_TYPE, targetField = "coamPersonGender")})
    public Result<IPage<MoperDutyMnPeople>> getReplacePeopleListByCondition(Page<MoperDutyMnPeople> page, MoperDutyMnPeople people){
        MoperDutyMnRota rota = new MoperDutyMnRota();
        rota.setCoamClassesId(people.getCoamClassesId());
        rota.setCoamGenerateDate(people.getCoamGenerateDate());
        List<MoperDutyMnPeople> dutyPersonByDateAndClasses = this.rotaMapper.getDutyPersonByDateAndClasses(rota);

        people.setIds(dutyPersonByDateAndClasses.stream().map(MoperDutyMnPeople::getCoamKid).collect(Collectors.toList()));
        IPage<MoperDutyMnPeople> peopleList =  peopleService.getDutyPeopleListByCondition(page, people);
        return Result.content(peopleList);
    }

    /**
     * 获取换班人员列表
     * @param people
     * @return
     */
    @GetMapping("/getExchangePeopleListByCondition")
    @Operation(summary = "获取换班人员列表")
    @DictTranslation({@FieldParam(dictType = DicConstant.DUTY_TYPE, targetField = "coamPersonType"),
            @FieldParam(dictType = DicConstant.SYSTEM_FLAG, targetField = "coamSysFlag"),
            @FieldParam(dictType = DicConstant.GENDER_TYPE, targetField = "coamPersonGender")})
    public Result<List<MoperDutyMnPeople>> getExchangePeopleListByCondition(MoperDutyMnPeople people){
        return Result.content(peopleService.getExchangePeopleListByCondition(people));
    }



}
